package com.example.httpsleuthclient;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Description:
 * @Author WuGang
 * @Date 2022/11/10 14:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/test")
public class A {
    @GetMapping("/login")
    public String login() throws IOException {
        URL url = new URL("http://127.0.0.1:8080/test/login");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.connect();
        InputStream is = null;
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        if (httpURLConnection.getResponseCode() == 200) {
            //获取返回的数据
            is = httpURLConnection.getInputStream();
            if (null != is) {
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String temp = null;
                while (null != (temp = br.readLine())) {
                    result.append(temp);
                }
            }
        }
//        restful风格也属于http请求
        return result.toString();
    }
}
